package com.buho29.rcmanager.widget;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Paint.Align;
import android.view.View;

import com.buho29.rcmanager.R;
import com.buho29.rcmanager.utils.Format;


public class Graph extends View {

	private Paint paint;
	private int[] mColors;
	private String mColumn;
	
	private float max ; 
	private float min ;
	private int[][] mValues;
	private Boolean drawed = false;
	
	
	public Graph(Context context,Cursor[] cur,String col, int[] colors) {
		super(context);
		
		mColors = colors;
		mColumn = col;
		
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setStrokeJoin(Paint.Join.MITER);
		paint.setStrokeWidth(1f);
		
		max = Integer.MIN_VALUE;
		min = Integer.MAX_VALUE;
		 
		mValues = new int[cur.length][];
		
		for (int i = 0; i < cur.length; i++){
			int count = cur[i].getCount();
			
			mValues[i]= new int[count];
			
			for (int j = 0; j < count; j++) {
				cur[i].moveToPosition(j);
				int value = cur[i].getInt(cur[i].getColumnIndexOrThrow(mColumn));
				mValues[i][j] = value;
				if (value > max) max = value;
				if (value < min) min = value;
			}
			cur[i].close();
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		
		if(drawed)return ;
		float gap = 25;
		float startx = 50;
		float height = getHeight();
		float width = getWidth()-1;
		float diff = max - min;
		float graphheight = height - (2 * gap);
		float graphwidth = width - (2 * gap);

		paint.setColor(Color.DKGRAY);
		paint.setStyle(Paint.Style.STROKE);
		canvas.drawLine(startx,gap, startx, graphheight+gap, paint);
		canvas.drawLine(width, gap, width, graphheight+gap, paint);


		paint.setStyle(Paint.Style.FILL);
		paint.setTextAlign(Align.LEFT);
		paint.setColor(Color.WHITE);
		canvas.drawText(getResources().getString(R.string.time), 4 ,10, paint);
		
		float stepy = graphheight/diff;
		
		float tmp = (graphheight) / 4 ;
		for (int i = 0; i < 5; i++) {
			float y = graphheight+gap - tmp * i;

			paint.setStyle(Paint.Style.STROKE);
			paint.setColor(Color.DKGRAY);
			canvas.drawLine(startx, y, width, y, paint);

			paint.setStyle(Paint.Style.FILL);
			paint.setColor(Color.WHITE);
			canvas.drawText(
					Format.formatSecondsToString((int)(((graphheight+gap - y) / stepy) + min)),
					0, y + 10, paint);
		}
		
		paint.setTextAlign(Align.CENTER);
		canvas.drawText(getResources().getString(R.string.drive), (graphwidth / 2) + startx, height - 4, paint);

		// draw drive
		if (diff>0) {
			
			for (int i = 0; i < mValues.length; i++){
				int count = mValues[i].length;
					
				paint.setColor(i<mColors.length ? mColors[i] : Color.WHITE);
				
				int value1 = mValues[i][0];
				
				float lastx = startx;
				float lasty = graphheight+gap - ((value1 - min)* stepy);
				float stepx = graphwidth / (count -1);
				
				Path p = new Path();
				p.moveTo(lastx, lasty);

				
				for (int j = 0; j < count; j++) {
					int value = mValues[i][j];
					
					float val = value - min;
					float x = startx + (j * stepx) ;
					float y = graphheight+gap - (val * stepy) ;
					
					p.lineTo(x, y);
				}
				
				
				paint.setStyle(Paint.Style.STROKE);
				canvas.drawPath(p, paint);
			}
		}
	}

}